package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.common.utils.CommonResponse;
import com.hdax.dm.common.utils.ResponseUtil;
import com.hdax.dm.constants.ResultEnumCode;
import com.hdax.dm.dtos.item.HotItemsDto;
import com.hdax.dm.dtos.item.ItemDescDto;
import com.hdax.dm.entity.item.DmItem;
import com.hdax.dm.exceptions.DmException;
import com.hdax.dm.exceptions.ItemException;
import com.hdax.dm.mappers.DmItemMapper;
import com.hdax.dm.mapstructs.HotItemDtoMapper;
import com.hdax.dm.mapstructs.ItemDescDtoMapper;
import com.hdax.dm.service.DmItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DmItemServiceImpl extends ServiceImpl<DmItemMapper, DmItem> implements DmItemService {
    @Autowired
    private HotItemDtoMapper dtoMapper;
    @Autowired
    private ItemDescDtoMapper descDtoMapper;

    @Override
    public List<HotItemsDto> hotItems(Long itemType1Id) {
        //封装查询条件
        QueryWrapper<DmItem> wrapper = new QueryWrapper<>();
        wrapper.eq("isRecommend",1L);
        wrapper.eq("itemType1Id",itemType1Id);
        //排序
        wrapper.orderByDesc("avgScore");
        wrapper.orderByDesc("startTime");

        //分页处理，默认查询5条数据
        IPage<DmItem> page = Page.of(1,5);

        page = this.page(page, wrapper);

        return dtoMapper.toTargetList(page.getRecords());
    }

    @Override
    public CommonResponse<ItemDescDto> getItems(Long itemId) {
        if(itemId == -1){
            //商品id请求异常
            throw new ItemException(ResultEnumCode.ITEM_NOT_EXISTS_ERR);
        }
        //查询商品详细属性信息
        ItemDescDto itemDescDto = descDtoMapper.toTarget(baseMapper.findItemById(itemId));

        return ResponseUtil.returnSuccess(itemDescDto);
    }
}
